package com.tim.javaApi;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description:
 * @Version 1.0, 2021/9/13
 * @Author Tim
 */
public class DateTimeApi {
    /**
     *
     */
    public static void concept(){
        // java8 以前
        Date date;
        Calendar calendar;

        // 新日期、时间
        LocalDate localDate;
        LocalTime localTime;// 精度-秒
        LocalDateTime localDateTime;
        Instant instant;// 时间戳，精度-纳秒。

        // 格式化
        DateTimeFormatter formatter;

        // 时间段
        Period period;// 精度-天
        Duration duration;// 精度-纳秒

        // 单位
        ChronoUnit unit;
    }

    /**
     * 本地日期、时间
     */
    public static void localDateTime(){
        // 获取方式
        LocalDate localDate = LocalDate.now();
        localDate = LocalDate.of(1, 2, 3);
        localDate = LocalDate.parse("2021-01-02", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        localDate = LocalDate.parse("2021-01-02", DateTimeFormatter.ISO_LOCAL_DATE);
        localDate = LocalDate.parse("2021-01-02");// 默认 DateTimeFormatter.ISO_LOCAL_DATE
//        localDate = LocalDate.

        LocalTime localTime = LocalTime.now();
        localTime = LocalTime.of(1, 2, 3);
        localTime = LocalTime.parse("01:02:03", DateTimeFormatter.ISO_LOCAL_TIME);

        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = LocalDateTime.of(2021, 03, 01, 1, 2, 3);
        localDateTime = LocalDateTime.parse("2020-01-02T01:02:03", DateTimeFormatter.ISO_LOCAL_DATE_TIME);


    }

    /**
     * 时间戳，精度-纳秒
     */
    public static void instant(){
        Instant instant = Instant.now();
        instant = Instant.parse("2007-12-03T10:15:30.00Z");// DateTimeFormatter.ISO_INSTANT 解析
        instant = Instant.ofEpochSecond(1234);// 相对1970-1-1
        instant = Instant.now(Clock.systemUTC());

    }

    /**
     * 时间段，精度-天
     */
    public static void period(){
        Period period = Period.between(LocalDate.now(), LocalDate.parse("2021-01-04"));
        period = Period.of(1, 2, 3);
        period = Period.ofDays(1);
        period = Period.parse("P1Y3W");// [-]PnYnMnWnD，Y年，M月，W周，D天；n可为负值

        int days = period.getDays();
    }

    /**
     * 时间段，精度-纳秒
     */
    public static void duration(){
        Duration duration = Duration.parse("P1DT1.23S");//PnDTnHnMn.fS，f可为小数
        duration = Duration.ofDays(2);
        duration = Duration.between(LocalDate.now(), LocalDate.parse("2021-01-04"));
        duration = Duration.between(Instant.ofEpochSecond(1), Instant.ofEpochSecond(5));

    }

    /**
     * 格式化器
     */
    public static void fomatter(){
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
        formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

        String str = formatter.format(LocalDate.now());
        TemporalAccessor ta = formatter.parse("yyyy-MM-dd");
    }

    /**
     *
     */
    public static void old2New(){
        Date date = new Date();
        Instant instant = date.toInstant();
        date = new Date(instant.toEpochMilli());

        Calendar calendar = Calendar.getInstance();
        instant = calendar.toInstant();

        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDate localDate = localDateTime.toLocalDate();
        localDate = LocalDate.ofEpochDay(instant.getEpochSecond()/(24*60*60));
        localDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

        LocalTime localTime = LocalTime.ofSecondOfDay(instant.getEpochSecond() % (24 * 60 * 60));

    }

    /**
     * 转Instant后，获取millis转Date
     */
    public static void new2Old(){
        LocalDate localDate = LocalDate.parse("2019-05-08");

        Instant instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        System.out.println(date);

        instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
        date = Date.from(instant);
        System.out.println(date);

        instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        date = Date.from(instant);
        System.out.println(date);

        instant = LocalDateTime.of(localDate, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
        date = Date.from(instant);
        System.out.println(date);

        instant = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)).toInstant();
        date = Date.from(instant);
        System.out.println(date);

        instant = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)).toInstant();
        date = Date.from(instant);
        System.out.println(date);

        Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT));
        date = new Date(timestamp.getTime());
        System.out.println(date);

    }

    public static void main(String[] args) throws ParseException {
//        LocalDate localDate = LocalDate.ofEpochDay(new Date().toInstant().getEpochSecond() / (24 * 60 * 60));
//        Period between = Period.between(LocalDate.now(), localDate);
//        System.out.println(between.isZero());
//        System.out.println(between.isNegative());// end-start；小于时为true
//        System.out.println(between.getDays());
//        between = Period.between(LocalDate.of(2023, 12, 1), localDate);
//        System.out.println(between.isZero());
//        System.out.println(between.isNegative());
//        System.out.println(between.getDays());

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr = sdf.format(now);
        String dateStr = sdf.format(sdf.parse("2022-1-1"));
        System.out.println(nowStr.compareTo(dateStr));

        long millis = Instant.now().toEpochMilli();
        Date date = new Date(millis);
        System.out.println(date);

        new2Old();
    }

    /**
     *
     */
    public static void dateCompare(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);// 通过calendar转换，避免0点问题
        LocalDate localDate = LocalDate.of(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH)+1, instance.get(Calendar.DAY_OF_MONTH));
        Period between = Period.between(LocalDate.now(), localDate);
        int result = between.isZero() ? 0
                : between.isNegative() ? -1
                : 1;

        // 先日期字符串比较，再Date比较
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr = sdf.format(now);
        String dateStr = sdf.format(date);
        result = nowStr.equals(dateStr) ? 0
                : now.before(date) ? -1
                : 1;

        // 纯日期比较；格式标准化：yyyy-MM-dd
        dateStr = "2022-11-1".replaceAll("-(\\d)-", "-0$1-")
                .replaceAll("-(\\d)$", "-0$1");
        result = nowStr.compareTo(dateStr);
    }
}
